package com.leyantech.ecp.agenx.web.testwin;

import com.leyantech.ecp.agenx.common.utils.PbPrintHelper;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

class TestWinReplySerializer extends StdSerializer<TestWinReply> {

  private static ObjectMapper objectMapper = new ObjectMapper();

  static {
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  }

  public TestWinReplySerializer() {
    this(null);
  }

  public TestWinReplySerializer(Class<TestWinReply> testWinReplyClass) {
    super(testWinReplyClass);
  }


  @Override
  public void serialize(TestWinReply testWinReply, JsonGenerator jsonGenerator,
      SerializerProvider serializerProvider) throws IOException {

    jsonGenerator.writeStartObject();

    if (testWinReply.getDialogueReplyV2() != null) {
      jsonGenerator.writeObjectField("dialogueReplyV2",
          objectMapper.readValue(PbPrintHelper.toJson(testWinReply.getDialogueReplyV2()),
              Object.class)
      );
    }

    if (testWinReply.getDialogueReplyContext() != null) {
      jsonGenerator.writeObjectField("dialogueReplyContext",
          objectMapper.readValue(PbPrintHelper.toJson(testWinReply.getDialogueReplyContext()),
              Object.class));
    }

    if (testWinReply.getPolicyDetail() != null) {
      jsonGenerator.writeObjectField("policyDetail",
          objectMapper.readValue(PbPrintHelper.toJson(testWinReply.getPolicyDetail()),
              Object.class));
    }
    if (testWinReply.getFaqDetail() != null) {
      jsonGenerator.writeObjectField("faqDetail",
          objectMapper
              .readValue(PbPrintHelper.toJson(testWinReply.getFaqDetail()), Object.class));
    }
    if (testWinReply.getWorkFlowDetail() != null) {
      jsonGenerator.writeObjectField("workFlowDetail",
          objectMapper.readValue(PbPrintHelper.toJson(testWinReply.getWorkFlowDetail()),
              Object.class));
    }
    if (testWinReply.getH2HDetail() != null) {
      jsonGenerator.writeObjectField("h2HDetail",
          objectMapper
              .readValue(PbPrintHelper.toJson(testWinReply.getH2HDetail()), Object.class));
    }
    if (testWinReply.getWaitDetail() != null) {
      jsonGenerator.writeObjectField("waitDetail",
          objectMapper
              .readValue(PbPrintHelper.toJson(testWinReply.getWaitDetail()), Object.class));
    }

    if (testWinReply.getTimestamp() != null) {
      jsonGenerator.writeNumberField("timestamp", testWinReply.getTimestamp());
    }

    jsonGenerator.writeBooleanField("testWinLimit", testWinReply.isTestWinLimit());

    if (testWinReply.getTestWinLimitReason() != null) {
      jsonGenerator.writeNumberField("testWinLimitReason",
          testWinReply.getTestWinLimitReason());
    }

    jsonGenerator.writeEndObject();
  }
}
